//
//  MXSStatusCell.swift
//  weibo
//
//  Created by 孙飞 on 15/12/11.
//  Copyright © 2015年 Sunfei. All rights reserved.
//

import UIKit
import SnapKit



class MXSStatusCell: UITableViewCell {
    
    // toolBar 顶部的约束
    var toolBarTopConstraint: Constraint?
    
    var statusViewModel: MXSStatusVM? {
        didSet{
            
            originalView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            
            toolBarTopConstraint?.uninstall()       //--> SnapKit 更新约束
            
            // 转发微博存在
            if statusViewModel?.status?.retweeted_status != nil {
                
                retweetView.hidden = false
                retweetView.statusViewModel = statusViewModel
                // 更新约束
                
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }else {
                // 转发微博不存在
                retweetView.hidden = true
                // 更新约束
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 设置视图
    private func setupUI(){
        // 1. 添加 View
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(statusToolBar)
        
        // 2. 添加约束
        // 原创微博的 View
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        // 转发微博
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom)
            make.trailing.equalTo(originalView)
        }
        
        
        // 底部 toolBar
        statusToolBar.snp_makeConstraints { (make) -> Void in
            self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.width.equalTo(contentView)
            make.height.equalTo(35)
        }
        
        // 非常重要,而约束不能少
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(statusToolBar)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
    }
    
    
    // MARK: - 懒加载
    private lazy var originalView: MXSStatusMajorView = {
    
        let view = MXSStatusMajorView()
        view.backgroundColor = UIColor.whiteColor()
        return view
    }()
    // 底部 toolBar
    private lazy var statusToolBar: MXSStatusToolBar = MXSStatusToolBar()
    // 转发微博 view
    private lazy var retweetView: MXSStatusRetweetView = MXSStatusRetweetView()
}

